package com.example.zhu95.mytimber.nowplaying;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.ConstantValue;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.activity.BaseActivity;
import com.example.zhu95.mytimber.entity.PlayQueueRecord;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.listener.OnMusicGestureListener;
import com.example.zhu95.mytimber.listener.OnPlayNextSongClickListener;
import com.example.zhu95.mytimber.listener.OnPlayPreviousSongClickListener;
import com.example.zhu95.mytimber.listener.PlayPauseBtnOnClickListener;
import com.example.zhu95.mytimber.listener.PlayPauseBtnOnTouchListener;
import com.example.zhu95.mytimber.listener.SeekBarChangeListener;
import com.example.zhu95.mytimber.utils.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import jp.co.recruit_lifestyle.android.widget.PlayPauseButton;

/**
 * @author zhu95
 * @date 2018/3/24
 */

public class Timber3 extends BaseTimber {

    int randomForeColor = Util.INSTANCE.getRandomForeColor();
    Unbinder unbinder;
    @BindView(R.id.img_timber3_header)
    ImageView imgTimber3Header;
    @BindView(R.id.tv_songName)
    TextView tvSongName;
    @BindView(R.id.tv_artist)
    TextView tvArtist;
    @BindView(R.id.img_shuffle)
    ImageView imgShuffle;
    @BindView(R.id.img_repeat)
    ImageView imgRepeat;
    @BindView(R.id.sb_progress)
    SeekBar sbProgress;
    @BindView(R.id.tv_current_time)
    TextView tvCurrentTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.img_previous_timber3)
    ImageView imgPreviousTimber3;
    @BindView(R.id.btn_play_pause_timber3)
    PlayPauseButton btnPlayPauseTimber3;
    @BindView(R.id.img_next_timber3)
    ImageView imgNextTimber3;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_timber3, container, false);
        unbinder = ButterKnife.bind(this, view);

        imgTimber3Header.setColorFilter(getResources().getColor(randomForeColor));
        btnPlayPauseTimber3.setColor(Color.WHITE);
        tvCurrentTime.setTextColor(getResources().getColor(R.color.colorGrayText));
        tvEndTime.setTextColor(getResources().getColor(R.color.colorGrayText));

        PlayPauseBtnOnClickListener clickListener = new PlayPauseBtnOnClickListener();
        PlayPauseBtnOnTouchListener touchListener = new PlayPauseBtnOnTouchListener(clickListener);
        btnPlayPauseTimber3.setOnTouchListener(touchListener);
        sbProgress.setOnSeekBarChangeListener(new SeekBarChangeListener(new SeekBarChangeListener.CallBack() {
            @Override
            public void updateOtherUI(int position) {
                tvCurrentTime.setText(Util.INSTANCE.getPlayTime(position));
            }
        }));
        imgPreviousTimber3.setOnClickListener(new OnPlayPreviousSongClickListener());
        imgNextTimber3.setOnClickListener(new OnPlayNextSongClickListener());
        OnMusicGestureListener onMusicGestureListener = new OnMusicGestureListener();
        onMusicGestureListener.enableDoubleClick(true);
        onMusicGestureListener.enableFling(true);
        final GestureDetector gestureDetector = new GestureDetector(rootActivity, onMusicGestureListener);
        imgTimber3Header.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDetector.onTouchEvent(event);
                return true;
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void updateUI(int songid, String musicState, int currentPosition) {
        updateHeaderUI(songid, musicState, currentPosition);
    }

    private void updateHeaderUI(int songid, String musicState, int currentPosition) {
        SongBean song = SongData.findSongByID(songid);
        if (song == null) {
            return;
        }

        tvSongName.setText(song.getTitle());
        tvArtist.setText(song.getArtist());
        sbProgress.setMax(song.getDuration());
        sbProgress.setProgress(currentPosition);
        tvCurrentTime.setText(Util.INSTANCE.getPlayTime(currentPosition));
        tvEndTime.setText(song.getPlayTime());

        String albumArt = song.getAlbumArt();
        updatePlayModeView();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(this).load(albumArt).centerCrop().into(imgTimber3Header);
        } else {
            Glide.with(this).load(R.drawable.ic_empty_music2).centerCrop().into(imgTimber3Header);
        }
        BaseActivity.updatePlayPauseBtnState(btnPlayPauseTimber3, musicState);
    }

    private void updatePlayModeView() {
        int playMode = PlayQueueRecord.Companion.getPlayModeFromDB();
        int colorAccent = getResources().getColor(R.color.colorAccent);
        if (playMode == ConstantValue.PLAYMODE_RANDOM) {
            imgShuffle.setColorFilter(colorAccent);
            imgRepeat.setColorFilter(Color.BLACK);
        } else if (playMode == ConstantValue.PLAYMODE_SINGLE) {
            imgRepeat.setColorFilter(colorAccent);
            imgShuffle.setColorFilter(Color.BLACK);
        } else {
            imgRepeat.setColorFilter(Color.BLACK);
            imgShuffle.setColorFilter(Color.BLACK);
        }
    }

    @Override
    public void updateProgress(int songid, final int currentPosition) {
        final SongBean song = SongData.findSongByID(songid);
        if (song != null) {
            rootActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    sbProgress.setMax(song.getDuration());
                    sbProgress.setProgress(currentPosition);
                    tvCurrentTime.setText(Util.INSTANCE.getPlayTime(currentPosition));
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.img_shuffle, R.id.img_repeat})
    public void onViewClicked(View view) {
        int playmode = PlayQueueRecord.Companion.getPlayModeFromDB();
        switch (view.getId()) {
            case R.id.img_shuffle: {
                if (playmode == ConstantValue.PLAYMODE_RANDOM) {
                    playmode = ConstantValue.PLAYMODE_SEQUENCE;
                } else {
                    playmode = ConstantValue.PLAYMODE_RANDOM;
                }
            }
            break;
            case R.id.img_repeat: {
                if (playmode == ConstantValue.PLAYMODE_SINGLE) {
                    playmode = ConstantValue.PLAYMODE_SEQUENCE;
                } else {
                    playmode = ConstantValue.PLAYMODE_SINGLE;
                }
            }
            break;
            default:
        }
        PlayQueueRecord playQueueRecord = new PlayQueueRecord();
        playQueueRecord.setPlayMode(playmode);
        if (playmode == ConstantValue.PLAYMODE_SEQUENCE) {
            playQueueRecord.setToDefault("playMode");
        }
        playQueueRecord.updateAll();
        updatePlayModeView();
    }
}
